{% extends "stocksys/base/base_page.html" %}
{% load staticfiles %}
{% block headscript %}
    <script>
        function cancel_order(order_id) {
            var token = $.cookie('csrftoken');
            let action = true;
            let data = {'order_id': order_id, 'action': action};
            if (order_id == 0) {
                action=false;
                data = {'action': action};
            }
            $.ajax({
                url: "{% url 'stocksys:cancel_order' %}",
                type: "DELETE",
                data: data,
                headers: {'X-CSRFToken': token},
                dataType: "json",
                success: function (data) {
                    if (data['status'] === 200) {
                        swal({
                            title: "撤单成功",
                            buttonsStyling: false,
                            confirmButtonClass: "btn btn-success",
                            type: "success"
                        }).catch(swal.noop);
                    } else {
                        swal({
                            title: "撤单失败",
                            buttonsStyling: false,
                            confirmButtonClass: "btn btn-success",
                            type: "error"
                        }).catch(swal.noop);
                    }
                },
                error: function (e) {
                    console.log(e);
                }
            });
        }
    </script>
{% endblock %}
{% block content %}
    <div class="content">
    <div class="container-fluid">
        <div class="row">
            <div class="content">
                <div class="btn-toolbar" role="toolbar">
                    <div class="btn-group btn-group-seperator">
                        <button onclick="cancel_order(0)" type="button" id="new_rule" class="btn btn-default "><i
                                class="ti-save">全部撤单</i></button>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-md-12">
                    <div class="card">
                        <div class="content">
                            <div class="material-datatables">
                                <table id="datatables" class="table table-striped table-no-bordered table-hover">
                                    <thead>
                                    <tr>
                                        <th>订单ID</th>
                                        <th>订单号</th>
                                        <th>订单类型</th>
                                        <th>股票名称</th>
                                        <th>价格</th>
                                        <th>订单状态</th>
                                        <th class="disabled-sorting text-center">操作</th>
                                    </tr>
                                    </thead>
                                    <tbody>
                                    {% for order in orders %}
                                        <tr id="{{ stock.code }}">
                                            <td>{{ order.id }}</td>
                                            <td>{{ order.order_id }}</td>
                                            <td>{{ order.order_type.type_name }}</td>
                                            <td>{{ order.stock_info.stock.name }}</td>
                                            <td>{{ order.price }}</td>
                                            <td>{{ order.state.state_name }}</td>
                                            {% if order.state.state == 0 %}
                                                <td class="td-actions text-center">
                                                    <p id="{% url 'stocksys:cancel_order' %}"
                                                       onclick="cancel_order('{{ order.order_id }}')"
                                                       rel="tooltip" title="撤单"
                                                       class="btn btn-simple btn-xs">
                                                        <i>撤单</i>
                                                    </p>
                                                </td>
                                            {% else %}
                                                <td class="td-actions text-center">
                                                </td>
                                            {% endif %}
                                        </tr>
                                    {% endfor %}
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
{% endblock %}
{% block pagescript %}
    <script type="text/javascript">
        $(document).ready(function () {
            $('#datatables').DataTable({
                "pagingType": "full_numbers",
                "lengthMenu": [
                    [10, 25, 50, -1],
                    [10, 25, 50, "All"]
                ],
                responsive: true,
                language: {
                    search: "_INPUT_",
                    searchPlaceholder: "检索关键字",
                }

            });
            var table = $('#datatables').DataTable();
        });
    </script>
{% endblock %}

